#include "exprange.h"
#include "ui_exprange.h"
#include<QtSql/QSqlQuery>
#include<QDebug>
#include<QDesktopWidget>


ExpRange::ExpRange(QDate from,QDate to,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ExpRange)
{
    ui->setupUi(this);
    f=from;
    t=to;
    QRect in=QApplication::desktop()->availableGeometry(this);
    ui->scrollArea->setGeometry(0,0,(in.width()),(in.height()));
   // ui->listWidget->setGeometry(0,75,(q.width()-10),(q.height()-30));
    ui->label_header->setFocus();
    int fontSize=10;
    ui->label_header->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
    ui->label_range->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->label_range->setText(QString("%1 and %2").arg(f.toString()).arg(t.toString()));
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );
     if(db.db_connect())
     {
         QSqlQuery q;
         int count=0;
         q.prepare(QString("select * from expense_table where exp_date >= ? and exp_date <= ?"));
         q.addBindValue(from);
         q.addBindValue(to);
         if(q.exec())
         {
             while(q.next())
             {
                 //QDate dd=QDate::fromString(q.value(3).toString(),"ddd MMM dd yyyy");
                 //qDebug()<<dd.month();
                 //if((from.operator <=(dd))&&(to.operator >=(dd)))//Greate than from and less than to
                 //{
                     item1=new QListWidgetItem(QIcon(":/myresources/images/img1.png"),QString("%1 : %2").arg(q.value(5).toString()).arg(q.value(1).toDouble()),ui->listWidget,QListWidgetItem::UserType);
                     item1->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
                     item1->setToolTip(QString("%1").arg(q.value(0).toString()));
                     count++;
                 //}
             }

         if(count==0)
         {
            // ui->label_header->setVisible(false);
             ui->listWidget->hide();
             //ui->label_range->setVisible(false);
            // ui->label_msg->setVisible(true);
             ui->label_header->setText(tr("No expense record found"));
         }
     }//Select
         db.db_close();

 }
}

ExpRange::~ExpRange()
{
    delete ui;
}
void ExpRange::back()
{
    (new ExpCustom)->show();
}
void ExpRange::on_listWidget_itemActivated(QListWidgetItem* item)
{
    ExpDetails *e=new ExpDetails(item->toolTip().toInt(),4);
    //Id 4 for custom
    e->setDate(f,t);
    e->show();
}
